<template>
    <div>
        <div class="e-table billList">
            <div class="top">
                <div class="left">
                    <div class="listTop">
                        <el-button
                            size="small"
                            class="btn-greenYellow"
                            @click="openAddDialog"
                        >
                            新增</el-button
                        >
                    </div>
                </div>
                <div class="right">
                    <div class="right-div1">
                        <el-input
                            placeholder="请输入关键字"
                            size="small"
                            v-model="pagination.keyword"
                            @blur="search"
                        >
                            <i slot="suffix" class="el-input__icon el-icon-search" @click="search"></i>
                        </el-input>
                    </div>
                    <div>
                        <el-button
                            size="small"
                            style="background: #2e61d7; color: #fff"
                            @click="heightSharch"
                            >高级搜索
                        </el-button>
                    </div>
                </div>
            </div>
            <el-table
                ref="table"
                class="table"
                :data="tableData"
                border
                style="width: 100%"
                highlight-current-row
                height="0"
                v-sortTable="{ 'tableData': tableData, '_this': this, 'ref': 'table' }"
            >
                <el-table-column label="维保编号" min-width="200" show-overflow-tooltip  class-name="blue-column">
                <template slot-scope="{ row }" >
                    <span @click="tableDbShow(row)">{{row.billNo}}</span>
                </template>
                </el-table-column>
                <el-table-column
                    prop="maintenanceType"
                    label="维保类型"
                    width="150"
                >
                    <template slot-scope="{ row }">
                        <span>{{  maintenanceType.find(x=>+x.value === row.maintenanceType).label  }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="billDate"
                    label="记录日期"
                    width="150"
                >
                </el-table-column>
                <el-table-column
                    prop="maintenanceUnit"
                    label="维保单位"
                    min-width="200"
                    show-overflow-tooltip
                    align="center"
                >
                </el-table-column>
                <el-table-column
                    prop="orgName"
                    label="机构"
                    align="center"
                    min-width="200"
                    show-overflow-tooltip
                >
                </el-table-column>
                <el-table-column
                    prop="maintenancePrice"
                    label="维保费（元）"
                    width="150"
                    align="center"
                >
                </el-table-column>
                <el-table-column
                    prop="state"
                    label="状态"
                    width="150"
                >
                    <template slot-scope="{ row }">
                        <span v-status="row.state">{{ statusLable(row.state) }}</span>
                    </template>
                </el-table-column>
            </el-table>
            <ComPagination
                :total="totalPage"
                :pageSize.sync="pagination.limit"
                :currentPage.sync="pagination.page"
                @currentChange="listByEntity"
                @sizeChange="listByEntity"
                class="bottomSetting"
            />
        </div>
    </div>
</template>

<script>
import request from '@/api/equip/dailyManagement.js'
import ComPagination from '@/components/pagination/pagination.vue'
import { mapState } from 'vuex'
import { vueDom, debounce, advanceSearch } from '@/utils/common.js'
import { getCurrentUnitInfo } from '@/api/equip/dailyManagementAudit.js'
export default {
    data () {
        return {
            tableData: [],
            pagination: {
                'endTime': '',
                'founderId': '',
                'keyword': '',
                'limit': 20,
                'loginOrgId': '',
                'orgId': localStorage.getItem('searchOrgId'),
                'page': 1,
                'startTime': '',
                'states': []
            },
            totalPage: 0, //总页数
            tableHeight: 0,
            auditState: {}, //流程按钮显示状态
            auditParams: {
                'billId': '',
                'billType': 3006,
                'freeNextUnitIndex': null,
                'freeNextUser': '',
                'isPass': true,
                'isShareInAudit': true,
                'opinion': '',
                'orgId': '',
                'orgName': '',
                'userId': '',
                'workId': ''
            },
            dialogVisibleSearch: false
        }
    },
    components: {
        ComPagination
    },
    computed: {
        ...mapState({
            maintenanceType: state => state.equip.equipData.maintenanceType, //维保类型
        }),
        statusLable () {
            return param=> {
                const state =  this.$store.state.equip.equipData.state
                const obj = state.find(x=>x.value === param)
                return obj.label
            }
        },
    },
    created () {
        this.getDicValue()
        this.getList()
    },
    mounted () {
        // table动态高度
        vueDom(
            () => {
                this.tableHeight = document.body.clientHeight - 170
            },
            () => {
                return $('table').length !== 0
            }
        )
        this.fn = () => {
            this.tableHeight = document.body.clientHeight - 170
        }
        window.onresize = debounce(this.fn)
    },
    methods: {
        //获取当前使用的字典
        getDicValue () {
            this.$store.dispatch('equip/setTimeUnitList')//更新计时单位
            this.$store.dispatch('equip/setUseType')//更新使用类型
            this.$store.dispatch('equip/setMeasurementMode')//更新计量方式
            this.$store.dispatch('equip/setRunningState')//更新运行状态
            this.$store.dispatch('equip/setMaintenanceType')//更新维保类型
            this.$store.dispatch('equip/setEquipStatus')//更新设备状态
        },
        getList () {
            request.maintenanceRecordsList(this.pagination).then(res=>{
                this.tableData = res.list
                this.totalPage = res.totalCount
            })
        },
        //分页查询
        listByEntity () {
            this.getList()
        },
        //模糊搜索
        search () {
            this.getList()
        },
        // 高级搜索关闭
        heightSharchClose () {
            this.dialogVisibleSearch = false
        },
        //清空高级搜索的数据
        clearData () {
            this.pagination = {
                'endTime': '',
                'founderId': '',
                'keyword': '',
                'limit': 10,
                'loginOrgId': '',
                'orgId': '',
                'page': 1,
                'startTime': '',
                'states': []
            }
            this.setSearchData({ equipSearchData: this.pagination })
        },
        heightSharch () {
            advanceSearch('/dailyManagementHeightSearch?type=3').then(res => {
                this.pagination = res.equipSearchData
                this.getList()
                this.clearData()
            })
        },
        //新增
        openAddDialog () {
            this.$router.push('/maintenanceRecordsAdd')
        },
        //查看
        tableDbShow (row) {
            if(row.state === -1 || row.state === 2 || row.state === 5) {
                this.$router.push({
                    path: '/maintenanceRecordsShow',
                    query: { billid: row.billId }
                })
            }else{
                this.auditParams.billId = row.billId
                getCurrentUnitInfo(this.auditParams).then(res => {
                    this.auditState = res
                    if( this.auditState.canModify) {
                        this.$router.push({
                            path: '/maintenanceRecordsEdit',
                            query: { billid: row.billId }
                        })
                    }else{
                        this.$router.push({
                            path: '/maintenanceRecordsShow',
                            query: { billid: row.billId }
                        })
                    }
                })
            }
        },
    }
}
</script>

<style lang="scss" scoped>
</style>
